package com.zx.designpattern.view.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.designpattern.R;
import com.zx.designpattern.base.BaseActivity;
import com.zx.designpattern.view.fragment.OneFragment;
import com.zx.designpattern.view.fragment.ThreeFragment;
import com.zx.designpattern.view.fragment.TwoFragment;
import com.zx.designpattern.utils.interfaceforall.FunctionManager;
import com.zx.designpattern.utils.interfaceforall.FunctionNoParamNoResult;
import com.zx.designpattern.utils.interfaceforall.FunctionWithParamAndResult;
import com.zx.designpattern.utils.interfaceforall.FunctionWithParamOnly;
import com.zx.designpattern.utils.interfaceforall.FunctionWithResultOnly;
import com.zx.designpattern.presenter.BasePresenter;

public class Interface4AllActivity extends BaseActivity implements View.OnClickListener {
    Fragment oneFragment;
    Fragment twoFragment;
    Fragment threeFragment;
    TextView bottom_one_btn;
    TextView bottom_two_btn;
    TextView bottom_three_btn;
    FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interface4_all);
        bottom_one_btn = findViewById(R.id.bottom_one_btn);
        bottom_two_btn = findViewById(R.id.bottom_two_btn);
        bottom_three_btn = findViewById(R.id.bottom_three_btn);
        bottom_one_btn.setOnClickListener(this);
        bottom_two_btn.setOnClickListener(this);
        bottom_three_btn.setOnClickListener(this);


        oneFragment = OneFragment.newInstance("", "");
        twoFragment = TwoFragment.newInstance("", "");
        threeFragment = ThreeFragment.newInstance("", "");

        mFragmentManager = getSupportFragmentManager();
    }


    @Override
    protected int getRootViewId() {
        return R.layout.activity_interface4_all;
    }


    /**
     * 增加接口
     */
    @Override
    protected FunctionManager addInterfaceForFragment() {
        //链式增加接口
        return FunctionManager.getInstance().addFunctionNoParamNoResult(new FunctionNoParamNoResult(OneFragment.INTERFACE_NAME) {//给OneFragment增加接口
            @Override
            public void function() {
                Toast.makeText(Interface4AllActivity.this, "addInterfaceForFragment 1 你好！没有返回值，没有参数的接口！", Toast.LENGTH_SHORT).show();
            }
        }).addFunctionWithParamAndResult(new FunctionWithParamAndResult<String, Integer>(TwoFragment.INTERFACE_NAME) {//给TwoFragment增加接口
            @Override
            public String function(Integer param) {
                return (param + 1) + "----你好！有返回值，有参数的接口！";
            }
        }).addFunctionWithParamOnly(new FunctionWithParamOnly<String>(ThreeFragment.INTERFACE_NAME) {//给TwoFragment增加接口
            @Override
            public void function(String param) {
                Toast.makeText(Interface4AllActivity.this, "addInterfaceForFragment 3 param is " + param, Toast.LENGTH_SHORT).show();

            }
        }).addFunctionWithResultOnly(new FunctionWithResultOnly<String>(ThreeFragment.INTERFACE_NAME) {//给ThreeFragment增加接口
            @Override
            public String function() {
                return "你好！只有返回值，没有参数的接口";
            }
        });
    }
    //移除接口
    protected void removeInterface() {
        FunctionManager.getInstance().clear(OneFragment.INTERFACE_NAME);
        FunctionManager.getInstance().clear(TwoFragment.INTERFACE_NAME);
        FunctionManager.getInstance().clear(ThreeFragment.INTERFACE_NAME);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    Fragment currentFragment;

    private void switchFragment(Fragment toFragment) {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

        if (!toFragment.isAdded()) {
            if (currentFragment != null)
                fragmentTransaction.hide(currentFragment);
            fragmentTransaction.add(R.id.content_fl, toFragment, toFragment.getClass().getName()).commit();
        } else {
            fragmentTransaction.hide(currentFragment).show(toFragment).commit();
        }
        currentFragment = toFragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_one_btn:
                switchFragment(oneFragment);
                break;
            case R.id.bottom_two_btn:
                switchFragment(twoFragment);
                break;
            case R.id.bottom_three_btn:
                switchFragment(threeFragment);
                break;
            default:
                break;
        }
    }


    @Override
    public void showView(int code, Object data) {

    }
}
